' ects_setup_adam.vbs
'
' Sets up ADAM for the External Collaboration Toolkit for SharePoint
'
' Takes three arguments, the base container for ADAM, the LDAP port, and the LDAPS port
'
' Return Codes:
'    0 - Successful completion
'    1 - Could not find ADAM utilities.  May indicate that ADAM is not installed.
'    2 - Could not find LDF files for extending ADAM.
'    3 - Could not create the ADAM instance
'    4 - Could not extend the ADAM schema
'    5 - Could not add additional objects to the ADAM instance
'    6 - Incorrect arguments provided
'

Set objFSO = Wscript.CreateObject("Scripting.FileSystemObject")
Set objShell = Wscript.CreateObject("Wscript.Shell")
Set colProcessEnvVars = objShell.Environment("Process")

'
' Parse arguments
'

Set args = Wscript.Arguments
If args.Count < 3 Then
    DisplaySyntax
    Wscript.Quit(6)
End If
    
strContainer = args.Item(0)
strLDAPPort = args.Item(1)
strLDAPSPort = args.Item(2)

'
' Set up file names
'

strAdamInstall = objFSO.BuildPath(colProcessEnvVars("windir"), "\adam\adaminstall.exe")
strLDIFDE =  objFSO.BuildPath(colProcessEnvVars("windir"), "\adam\ldifde.exe")
strSchemaImportFile = "ectsschemaextensions.ldf"
strImportFile = "ectsdirectoryextensions.ldf"

'
' Ensure that all files are where they need to be
'

if Not objFSO.FileExists(strAdamInstall) Then
    Wscript.Echo("Can't find " + strAdamInstall + "! Is ADAM installed?")
    Wscript.Quit(1)
End If

if Not objFSO.FileExists(strLDIFDE) Then
    Wscript.Echo("Can't find " + strLDIFDE + "! Is ADAM installed?")
    Wscript.Quit(1)
End If

if Not objFSO.FileExists(strSchemaImportFile) Then
    Wscript.Echo("Can't find " + strSchemaImportFile + ".  Please verify that this file exists")
    Wscript.Echo("in the current directory.")
    Wscript.Quit(2)
End If

if Not objFSO.FileExists(strImportFile) Then
    Wscript.Echo("Can't find " + strImportFile + ".  Please verify that this file exists")
    Wscript.Echo("in the current directory.")
    Wscript.Quit(2)
End If

'
' Create the answer file for unattended ADAM install
'

strFileName = objFSO.GetTempName
strTempDir = colProcessEnvVars("TEMP")
strAnswerFileName = objFSO.BuildPath(strTempDir, strFileName)
Set objAnswerFile = objFSO.CreateTextFile(strAnswerFileName)
objAnswerFile.writeLine("[ADAMInstall]")
objAnswerFile.writeLine("; The following line specifies to install a unique ADAM instance.")
objAnswerFile.writeLine("InstallType=Unique")
objAnswerFile.writeLine("; The following line specifies the name to be assigned to the new instance.")
objAnswerFile.writeLine("InstanceName=ECTSInstance")
objAnswerFile.writeLine("; The following line specifies the communications port to use for LDAP.")
objAnswerFile.writeLine("LocalLDAPPortToListenOn=" + strLDAPPort)
objAnswerFile.writeLine("; The following line specifies the communications port to use for LDAPS.")
objAnswerFile.writeLine("LocalSSLPortToListenOn=" + strLDAPSPort)
objAnswerFile.writeLine("; The following line specifies an application partition to create")
objAnswerFile.writeLine("NewApplicationPartitionToCreate=""" + strContainer + """")
objAnswerFile.writeLine("; The following line specifies the .ldf files to import into the ADAM schema.")
objAnswerFile.writeLine("ImportLDIFFiles=""ms-user.ldf""")
objAnswerFile.Close

'
' Echo input parameters
'

Wscript.Echo("Running: cscript ects_setup_adam.vbs " + strContainer + " " + strLDAPPort + " " + strLDAPSPort)
Wscript.Echo("")

'
' Create the ADAM instance
'

Wscript.Echo("Creating the instance...")
intReturn = objShell.Run(strAdamInstall + " /answer:" + strAnswerFileName, 7, True)

objFSO.DeleteFile(strAnswerFileName)

If intReturn = 0 Then
    Wscript.Echo("Done!")
    Wscript.Echo("")
Else
    Wscript.Echo("ERROR: Could not create the ADAM instance.  Script aborting...")
    Wscript.Echo("")
    Wscript.Quit(3)
End If

'
' Extend the schema
'

Wscript.Echo("Extending the schema...")
intReturn = objShell.Run(strLDIFDE + " -i -f " + strSchemaImportFile + " -s " + GetHostName() + _
    " -t " + strLDAPPort + " -c ""cn=configuration,dc=x"" ""#configurationNamingContext""", 7, True)

If intReturn = 0 Then
    Wscript.Echo("Done!")
    Wscript.Echo("")
Else
    Wscript.Echo("ERROR: Could not extend the ADAM schema.  Script aborting...")
    Wscript.Echo("")
    Wscript.Quit(4)
End If

'
' Add additional objects
'

Wscript.Echo("Adding additional objects...")
intReturn = objShell.Run(strLDIFDE + " -i -f " + strImportFile + " -s " + GetHostName() + _
    " -t " + strLDAPPort + " -c ""CN=ExternalUserBranch,DC=X"" """ + strContainer + """", 7, True)

If intReturn = 0 Then
    Wscript.Echo("Done!")
    Wscript.Echo("")
Else
    Wscript.Echo("ERROR: Could not add additional objects.  Script aborting...")
    Wscript.Echo("")
    Wscript.Quit(5)
End If

'
' Done!
'

Wscript.Echo("ADAM setup complete.")
Wscript.Echo("")

Wscript.Quit(0)

'
' Display the syntax for the script
'

Sub DisplaySyntax
    Wscript.Echo("")
    Wscript.Echo("ects_setup_adam.vbs - Set up ADAM for the ECTS")
    Wscript.Echo("")
    Wscript.Echo("Usage: cscript ects_setup_adam.vbs container LDAPPort LDAPSPort")
    Wscript.Echo("")
    Wscript.Echo("Where: ""container""    is the base container for the LDAP instance,")
    Wscript.Echo("                      such as ""CN=ExternalUsers,DC=Woodgrovebank,DC=COM""")
    Wscript.Echo("       ""LDAPPort""     is the port number that ADAM will listen on for")
    Wscript.Echo("                      unecrypted connections")
    Wscript.Echo("Where: ""LDAPSPort""    is the port number that ADAM will listen on for")
    Wscript.Echo("                      encrypted connections")
    Wscript.Echo("")
    Wscript.Echo("Notes: [1] The script assumes that ADAM is installed in ""%windir%\ADAM"".")
    Wscript.Echo("           If it is not, please update the script before running.")
    Wscript.Echo("")
    Wscript.Echo("       [2] The script assumes you are running from the directory containing")
    Wscript.Echo("           the LDF files for extending the schema, etc.")
    Wscript.Echo("")
End Sub

'
' GetHostname function
'

Function GetHostname()

    Set objNTInfo = CreateObject("WinNTSystemInfo")
    GetHostname = objNTInfo.ComputerName

    Set objNTInfo = Nothing
    
End Function